package com.friendc.Controller;

import com.alibaba.fastjson.JSONObject;
import com.friendc.PrJo.User;
import com.friendc.Service.UserService;
import com.friendc.Util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.Style;
import java.io.IOException;

@WebServlet(name = "LoginController")
//@WebServlet("/cartshopnumadd")如此书写便不再需要配置web.xml文件
public class LoginController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//设置请求域编码格式
        //设置响应编码格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String name=request.getParameter("name");
       // System.out.print(name);
        UserService user=new UserService();
        User use=user.login(username,password);
        //String user1= JSONObject.toJSONString(use);//对象转json
        if (use!=null){
            HttpSession session=request.getSession();//session是一种会话，只要浏览器没有关闭，便一直存在
            session.setAttribute("user",use);
            request.getRequestDispatcher("WEB-INF/view/Home.jsp").forward(request,response);
        }else{
            request.getRequestDispatcher("WEB-INF/view/login.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
